package com.PictureSite.controller;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.naming.ConfigurationException;
import javax.servlet.http.HttpServletResponse;

import com.PictureSite.DatabaseAccess.PictureSiteCreatePostDAO;
import com.PictureSite.model.PictureSitePostModel;
import com.PictureSite.model.UserPost;
import com.google.appengine.api.datastore.Entity;

import freemarker.template.Configuration;
import freemarker.template.Template;

public abstract class PictureSiteController {
	

 		public final void bindModel(Object model,String templateName,HttpServletResponse resp) throws Exception {
			Configuration configuration = new Configuration(); 
			Template template = configuration.getTemplate("html/"+templateName);
			PrintWriter output = resp.getWriter();
			template.process(model, output);
		}
 		
 		public void getTopPosts(PictureSitePostModel model, PictureSiteCreatePostDAO dataStore) {
 			List<UserPost> topPosts = new ArrayList<UserPost>();
 			List<Entity> tempList = dataStore.getTopFiveRatedPost();
 			for (Entity row : tempList) {
 				UserPost topPost = new UserPost();
 				topPost.setTitle(row.getProperty("title").toString());
 				topPost.setUserName(row.getProperty("userName").toString());
 				topPost.setBlobId(row.getProperty("blobId").toString());
 				topPost.setId(String.valueOf(row.getKey().getId()));
 				topPosts.add(topPost);
 			}
 			model.setTopPosts(topPosts);
 		}
}
